package com.hugang.aboutstring;

import org.junit.Test;

/**
 * 常见string算法题
 *
 * @author hg
 * @date 2020/6/17 11:19
 */
public class StringAirthmetic {

    /**
     * 1、模拟String类的trim()方法
     */
    @Test
    public void test1() {
        String str = "  h e   ll   o w    o rld   ";
        char[] chars = str.toCharArray();
        int startIndex = 0;
        int endIndex = str.length() - 1;
        for (int i = startIndex; i < chars.length; i++) {
            if (' ' != chars[i]) {
                startIndex = i;
                break;
            }
        }
        for (int i = endIndex; i >= 0; i--) {
            if (' ' != chars[i]) {
                endIndex = i + 1;
                break;
            }
        }
        str = str.substring(startIndex, endIndex);
        System.out.println(str);
    }

    /**
     * 2、将字符串中指定部分进行反转，如 abcdefg ---> afedcbg
     */
    @Test
    public void test2() {
        String str = "abcdefg";
        int startIndex = 2;
        int endIndex = 4;

//        String str1 = str.substring(0, startIndex);
//        String str2 = str.substring(endIndex + 1);
//
//        String str3 = str.substring(startIndex, endIndex + 1);
//
//        char[] chars = str3.toCharArray();
//        for (int i = chars.length - 1; i >= 0; i--) {
//            str1 += chars[i];
//        }
//        str1 += str2;
//        System.out.println(str1);

//        char[] chars = str.toCharArray();
//        for (int i = startIndex, j = endIndex; i < j; i++, j--) {
//            char tmp = chars[i];
//            chars[i] = chars[j];
//            chars[j] = tmp;
//        }
//
//        System.out.println(new String(chars));

        StringBuilder stringBuilder = new StringBuilder(str.substring(startIndex, endIndex + 1));
        stringBuilder.reverse();

        String str1 = str.substring(0, startIndex);
        String str2 = str.substring(endIndex + 1);
        System.out.println(str1 + stringBuilder.toString() + str2);


    }

    /**
     * 3、获取一个字符串在另一个字符串中出现的次数，如"ab"在"abkkcadaabacaccdadgetadcabaaa"中出现的次数
     */
    @Test
    public void test3() {
        String str1 = "ab";
        String str2 = "";
//        int count = 0;
//        while (true){
//            if (str2.contains(str1)){
//                count ++;
//                str2 = str2.substring(str2.indexOf(str1) + str1.length());
//            } else {
//                break;
//            }
//        }

        int count = str2.split(str1).length - 1;
        System.out.println(count);
    }

    /**
     * 4、获取两个字符串的最大相同子串，如str1 = "asfhdslahellovnsageead", str2 = "vbgaanhellocnv"
     */
    @Test
    public void test4() {
        String str1 = "asfhdslahellosvnshhello11ageead";
        String str2 = "vbgaanhellovvsvsvshello11cnv";
        String tmp = "";
        boolean flag = false;

        //分析：从短字符串入手，进行长度递减的子串与较长的字符串比较
        //长度减一的子串：从开头减一个或从结尾减一个
        //长度减二的子串：从开头减两个，或从开头和结尾各减一个，或从结尾减两个
        //长度减三的子串：从开头减三个，或从开头减两个，从结尾减一个，或从开头减一个，从结尾减两个，或从结尾减三个
//        str2.substring(3, str2.length() - 0);
//        str2.substring(2, str2.length() - 1);
//        str2.substring(1, str2.length() - 2);
//        str2.substring(0, str2.length() - 3);
        //...
        if (str1.contains(str2)) {
            System.out.println(str2);
        } else {
            for (int i = 1; i < str2.length(); i++) {
                if (flag) {
                    break;
                }
                for (int j = 0; j <= i; j++) {
                    tmp = str2.substring(i - j, str2.length() - j);
                    if (str1.contains(tmp)) {
                        System.out.println(tmp);
                        flag = true;
                        break;
                    }
                }
            }
        }
    }

    /**
     * 对字符串中字符进行自然顺序排序
     */
    @Test
    public void test5() {
        String str = "qwertyuioplkjhgfdsazxcvbnm";

        char[] chars = str.toCharArray();
        long time1 = System.currentTimeMillis();
//        Arrays.sort(chars);

        for (int i = 0; i < chars.length; i++) {
            for (int j = 0; j < chars.length - i - 1; j++) {
                if (chars[j] > chars[j + 1]) {
                    char tmp = chars[j];
                    chars[j] = chars[j + 1];
                    chars[j + 1] = tmp;
                }
            }

        }

//        for (int i = 0; i < chars.length; i++) {
//            for (int j = i + 1; j < chars.length; j++) {
//                if (chars[i] > chars[j]){
//                    char tmp = chars[i];
//                    chars[i] = chars[j];
//                    chars[j] = tmp;
//                }
//            }
//
//        }

        long time2 = System.currentTimeMillis();

        System.out.println(new String(chars) + "，耗时:" + (time2 - time1) + "ms");
    }

    @Test
    public void test6() {
        System.out.println((char) 97);
    }
}

